/**
 * Copyright (c) 2012-2014 All Rights Reserved.
 */
package com.easy.framwork.jdbc.query;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author wy
 * @version v 0.1 2014-1-3 下午12:20:45 wy Exp $
 */
public class Insert implements Query{

    /** table name */
    private final String table;

    /** set sql */
    private final StringBuilder columns;

    /** set param */
    private final List<Object> paramList;

    /**
     * @param table
     */
    public Insert(String table) {
        this.table = table;
        columns = new StringBuilder();
        paramList = new ArrayList<Object>();
    }

    /**
     * set update
     * 
     * @param column
     * @param value
     * @return
     */
    public Insert set(String column, Object value) {

        if (columns.length() > 0) {
            columns.append(",");
        }
        columns.append(column);

        paramList.add(value);

        return this;
    }

    /**
     * Getter method for property <tt>where</tt>.
     * 
     * @where
     */
    public String getSQL() {
        StringBuilder sql = new StringBuilder(table.length() + columns.length() + paramList.size()
                                              + 20);
        sql.append("INSERT INTO ").append(table);
        sql.append("(").append(columns).append(") ").append("VALUES").append("(");

        int i = 0;
        while (i++ < paramList.size()) {
            if (i > 1) {
                sql.append(",");
            }
            sql.append("?");
        }
        sql.append(")");
        return sql.toString();
    }

    /**
     * Getter method for property <tt>getParams</tt>.
     * 
     * @return paramList
     */
    public Object[] getParams() {
        return paramList.toArray();
    }

}
